<template>
  <div class="label-tag">
    <template v-if="mergedConfig">
      <div class="label-item" v-for="(label, i) in mergedConfig.data" :key="label">
        {{ label }} <div :style="`background-color: ${mergedConfig.colors[i % mergedConfig.colors.length]};`" />
      </div>
    </template>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref, watch } from 'vue'
import { deepMerge } from '@jiaminghi/charts/lib/util/index.js'
import { deepClone } from '@jiaminghi/c-render/lib/plugin/util.js'

interface LabelTagConfig {
  /**
   * @description Label data
   */
  data?: string[]
  /**
   * @description Label colors (Hex|Rgb|Rgba|color keywords)
   */
  colors?: string[]
}

const props = defineProps<{
  config?: LabelTagConfig
}>()

// 默认配置（带完整类型）
const defaultConfig = reactive<Required<LabelTagConfig>>({
  data: [],
  colors: ['#00baff', '#3de7c9', '#fff', '#ffc530', '#469f4b']
})

// 合并后的配置（使用 ref 保持响应性）
const mergedConfig = ref<Required<LabelTagConfig> | null>(null)

// 配置合并方法
const mergeConfig = () => {
  mergedConfig.value = deepMerge(
    deepClone(defaultConfig, true),
    props.config || {}
  ) as Required<LabelTagConfig>
}

// 监听配置变化
watch(() => props.config, mergeConfig)

// 初始化时合并配置
onMounted(mergeConfig)

</script>

<style lang="less">
.label-tag {
  display: flex;
  justify-content: center;
  align-items: center;

  .label-item {
    margin: 5px;
    font-size: 15px;
    display: flex;
    align-items: center;

    div {
      width: 12px;
      height: 12px;
      margin-left: 5px;
    }
  }
}
</style>
